﻿using System.Web.Security;
using System.Web;
using System;
//using msBsc.OnlineUser;
//using yaf;

namespace msBsc.AppLogic.Cfg
{
    /// <summary>
    /// UserInfo 的摘要说明
    /// </summary>
    public class BseUserInfo
    {
        public BseUserInfo()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static string strDomanName="msBscDoman";
        public static bool IsWebSys = true;
        private static string _Operator = "";
        /// <summary>
        /// 当前登录用户
        /// </summary>
        public static string Operator
        {
            get
            {
                if (IsWebSys)
                {
                    if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        string stem11 = System.Web.HttpContext.Current.User.Identity.Name;
                        if (!string.IsNullOrEmpty(stem11))
                        {
                            return stem11.Split('|')[0];
                        }
                    }
                    return "";
                }
                else
                {
                    return _Operator;
                }
            }
            set
            {
                _Operator = value;
            }
        }

        private static string _OperatorName = "";
        /// <summary>
        /// 用户姓名
        /// </summary>
        public static string OperatorName
        {
            get
            {
                if (IsWebSys)
                {
                    if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        string stem11 = System.Web.HttpContext.Current.User.Identity.Name;
                        if (stem11.IndexOf('|') != -1)
                        {
                            return stem11.Split('|')[3];
                        }
                    }
                    return "";
                }
                else
                {
                    return _OperatorName;
                }
            }
            set
            {
                _OperatorName = value;
            }
        }

        private static string _OperRank = "";
        /// <summary>
        /// 用户权限级别
        /// </summary>
        public static string OperRank
        {
            get
            {
                if (IsWebSys)
                {
                    if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        string stem11 = System.Web.HttpContext.Current.User.Identity.Name;
                        if (stem11.IndexOf('|') != -1)
                        {
                            return stem11.Split('|')[2];
                        }
                    }
                    return "";
                }
                else
                {
                    return _OperRank;
                }
            }
            set
            {
                _OperRank = value;
            }
        }


        private static string _OperType = "";
        /// <summary>
        /// 用户类型
        /// </summary>
        public static string OperType
        {
            get
            {
                if (IsWebSys)
                {
                    if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        string stem11 = System.Web.HttpContext.Current.User.Identity.Name;
                        if (stem11.IndexOf('|') != -1)
                        {
                            return stem11.Split('|')[1];
                        }
                    }
                    return "";
                }
                else
                {
                    return _OperType;
                }
            }
            set
            {
                _OperType = value;
            }
        }

         

        //public string GetParamValue(string strCallBackParam, string strParam)
        //{
        //    if (string.IsNullOrEmpty(strCallBackParam))
        //        return string.Empty;
        //    else
        //    {
        //        string[] arrParam = strCallBackParam.Split(new char[] { ';' });
        //        for (int i = 0; i < arrParam.Length; i++)
        //        {
        //            string[] arrValue = arrParam[i].Split(new char[] { '|' });
        //            if (arrValue.Length > 0)
        //            {
        //                if (arrValue[0].Trim().ToUpper() == strParam.Trim().ToUpper())
        //                {
        //                    if (arrValue.Length > 1)
        //                        return arrValue[1];
        //                    else
        //                        return string.Empty;
        //                }
        //            }
        //        }
        //    }
        //    return string.Empty;
        //}


        public static bool Online
        {
            get
            {
                return Operator != "";
            }
        }


        public static void doSetAuthCookie(string strpUserCde, string strpoperType, string strpoperRank, string strUserNme = "")
        {
            //if (strUserNme == string.Empty)
            //{
            //    strUserNme = strpUserCde;
            //}
            string loginName = string.Format("{0}|{1}|{2}|{3}", strpUserCde, strpoperType, strpoperRank, strUserNme);




            //设置用户的 cookie 的值
            FormsAuthentication.SetAuthCookie(loginName, false);
            //获取用户的 cookie 
            HttpCookie cookie = FormsAuthentication.GetAuthCookie(loginName, false);
            //给用户的 cookie 的值加上 cookie 的域 和 过期日期
            //向客户端重写同名的 用户 cookie
            FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(cookie.Value);
            FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(1,
            oldTicket.Name,
            oldTicket.IssueDate,
            DateTime.Now.AddMinutes(30),
            oldTicket.IsPersistent,
            oldTicket.UserData,
            FormsAuthentication.FormsCookiePath);
            cookie.Domain = BseUserInfo.strDomanName;// "msBscDomain";
            cookie.Value = FormsAuthentication.Encrypt(newTicket);
            HttpContext.Current.Response.Cookies.Add(cookie);





            System.Web.Security.FormsIdentity identity = new System.Web.Security.FormsIdentity(newTicket);
            string[] roles = { "*" }; //System.Web.Security.Roles.GetRolesForUser(identity.Name); 注意这里，如果你没有启用asp.net权限系统，会报错。
            System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(identity, roles);
            HttpContext.Current.User = principal;





            //FormsAuthentication.SetAuthCookie(idName, false, FormsAuthentication.FormsCookiePath);
            //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, idName, System.DateTime.Now, System.DateTime.Now.AddMinutes(20), false, strUserData);
            //// generate new identity
            //FormsIdentity identity = new FormsIdentity(ticket);
            //System.Web.HttpCookie cookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
            //// write to client.
            //System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

            //FormsAuthentication.SetAuthCookie(idName, false);
        }

        //public void doSetAuthUserData(string strUserData)
        //{
        //    string idName = System.Web.HttpContext.Current.User.Identity.Name;
        //    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, idName, System.DateTime.Now, System.DateTime.Now.AddMinutes(20), false, strUserData);

        //    if (ticket != null)
        //    {
        //        System.Web.Security.FormsIdentity identity = new System.Web.Security.FormsIdentity(ticket);
        //        string[] roles = { "*" }; //System.Web.Security.Roles.GetRolesForUser(identity.Name); 注意这里，如果你没有启用asp.net权限系统，会报错。
        //        System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(identity, roles);
        //        System.Web.HttpContext.Current.User = principal;
        //        System.Web.HttpCookie cookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
        //        // write to client.
        //        System.Web.HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName] = FormsAuthentication.Encrypt(ticket);
        //    }

        //    //// generate new identity
        //    //FormsIdentity identity = new FormsIdentity(ticket);
        //}

        public static void ExitSys()
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName];
            cookie.Domain = BseUserInfo.strDomanName;//"msBscDomain";
            cookie.Value = null;
            cookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
            FormsAuthentication.SignOut();



            ////LogUserOperate("退出系统", "用户主动退出系统！");
            ////UpdateOnlineUserList(HttpContext.Current.Application, HttpContext.Current.Session.SessionID);
            ////string strtem1 = Operator; 
            //FormsAuthentication.SignOut();
            ////HttpContext.Current.Session.Abandon();

            ////FormsAuthentication.SetAuthCookie("", false);
            //System.Web.HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
            //System.Web.HttpContext.Current.Session.Clear();
            ////FormsAuthentication.SetAuthCookie("*", false);
            ////HttpContext.Current.Response.Redirect("Exit.htm", true);

            ////msBsc.SvRef.OnlineUser.oOnlineUser.RemoveLoginUsr(strtem1,true);

        }


    }
    //5_1_a_s_p_x.c_o_m
}